window.onload=init;

var bXmlHttpSupport = (typeof XMLHttpRequest != "undefined" ||window.ActiveXObject);
     
if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {
	function XMLHttpRequest() {
    var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
    "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
    "Microsoft.XMLHTTP"];
                             
    	for (var i=0; i < arrSignatures.length; i++) {
    		try {        
        		var oRequest = new ActiveXObject(arrSignatures[i]);            
            	return oRequest;        
        	} catch (oError) { /*ignore*/ }
    	}          
    
    	throw new Error("MSXML is not installed on your system.");               
    }
}    


function init(){
	//window.location.replace(ctx + '/jtext/listJtext');// 不可取，页面一直刷新
	//window.location.href= ctx + '/jtext/listJtext';// 不可取，页面一直刷新
}

function deleteRecord(obj_name) {
	var code_Values = document.all[obj_name];
	var ids = [];
	
	for ( var i = 0; i < code_Values.length; i++) {
		if(code_Values[i].checked == true){
			ids.push(code_Values[i].value);
		}
	}
	if(ids.length == 0){
		//说明一条记录也没有选中
		alert('请选择要删除的数据');
		return;
	}
	
	var idsStr = '';
	for(var i=0;i<ids.length;i++){
		idsStr += ids[i];
		if(i !== (ids.length-1)){
			idsStr += ',';
		}
	}
//	for(i=0;i<document.all(obj).length;i++)    
//	{    
//	if(!document.all(obj)[i].checked)alert("第"+(i+1)+"个没有选择")    
//	}
	
	if(bXmlHttpSupport) {
		var sUrl = ctx + "/jtext/deleteJtext?idsStr="+idsStr;
		var oRequest = new XMLHttpRequest();
		oRequest.onreadystatechange = function() {
			if(oRequest.readyState == 4) {
//				var tr = document.getElementById('tr_'+id);
//				var tbody = document.getElementById('tbody');
//				tbody.removeChild(tr);
				
				//window.location.reload(false);//参数 是否从缓存中读取
				//不可以使用reload，因为此时的url还是delete
				window.location.replace(ctx + '/jtext/listJtext');
			}
		};
		oRequest.open('POST', sUrl);
		oRequest.send(null);
	}
	 
}

function clickAllCheckbox(obj_name){
	var code_Values = document.all[obj_name];
	var checked = [];
	for ( var i = 0; i < code_Values.length; i++) {
		if(code_Values[i].checked == true){
			checked.push(code_Values[i]);
		}
	}
	if(checked.length<10){
		//没有全部选中
		checkAll(obj_name);
	}else{
		uncheckAll(obj_name);
	}
}

function checkAll(obj_name) {
	var code_Values = document.all[obj_name];
	if (code_Values.length) {
		for ( var i = 0; i < code_Values.length; i++) {
			code_Values[i].checked = true;
		}
	} else {
		code_Values.checked = true;
	}
}

function uncheckAll(obj_name) {
	var code_Values = document.all[obj_name];
	if (code_Values.length) {
		for ( var i = 0; i < code_Values.length; i++) {
			code_Values[i].checked = false;
		}
	} else {
		code_Values.checked = false;
	}
} 

function editRecord(obj_name){
	var code_Values = document.all[obj_name];
	var ids = [];
	
	for ( var i = 0; i < code_Values.length; i++) {
		if(code_Values[i].checked == true){
			ids.push(code_Values[i].value);
		}
	}
	if(ids.length == 0){
		//说明一条记录也没有选中
		alert('请选择要编辑的数据');
		return false;
	}else if(ids.length > 1){
		//选择的记录多余1条
		alert('只能选择一条记录进行编辑');
		return false;
	}else if(ids.length == 1){
		window.location.href = ctx + '/jtext/prepareEditJtext?jtext.id='+ids[0];
		return true;
	}
}

function gotoPage(index){
	var info = document.getElementsByName('info')[0].value;
	var url = ctx + '/jtext/listJtext?page.index=' + index + '&page.size=10';
	if(info != null && info != ''){
		url += '&info='+info;
	}else{
		url += '&info=""';
	}
	window.location.href = url;
}

function search(){
//	var info = document.getElementsByName('info')[0].value;
//	//info = uniencode(info);
//	var url = ctx + '/jtext/listJtext?page.index=1&page.size=10';
//	if(info != null && info != ''){
//		url += '&info='+info;
//	}else{
//		url += '&info=""';
//	}
//	window.location.href = url;
	gotoPage(1);
}
